package com.lff.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lff.domain.Bill;
import com.lff.domain.Provider;
import com.lff.service.BillService;
import com.lff.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("bill")
public class BillController {
    @Autowired
    private BillService billService;
    @Autowired
    private ProviderService providerService;

    @RequestMapping("showBills.do")
    public String showBills(Model model, @RequestParam(value = "pageIndex", defaultValue = "1") Integer startPage, HttpServletRequest request) {
        //可带参数 queryProviderId&queryProductName=a&queryIsPayment=1
        //获取可变参数 queryProviderId  queryProductName queryIsPayment
        //供应商id
        String strQueryProviderId = request.getParameter("queryProviderId");
        Long queryProviderId = null;
        if (strQueryProviderId != null) {
            queryProviderId = Long.valueOf(strQueryProviderId);
        }
        //付款状态
        String strQueryIsPayment = request.getParameter("queryIsPayment");
        Long queryIsPayment = null;
        if (strQueryIsPayment != null) {
            queryIsPayment = Long.valueOf(strQueryIsPayment);
        }
        //商品名
        String queryProductName = request.getParameter("queryProductName");
        PageHelper.startPage(startPage, 5);
        List<Bill> billList = billService.findAll(queryProviderId, queryIsPayment, queryProductName);
        PageInfo<Bill> pageInfo = new PageInfo<Bill>(billList);
        //获取供应商信息
        List<Provider> providerList = providerService.findAll(null, null);
        model.addAttribute("providerList", providerList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("billList", billList);
        model.addAttribute("queryProviderId", queryProviderId);
        model.addAttribute("queryIsPayment", queryIsPayment);
        model.addAttribute("queryProductName", queryProductName);
        return "billlist";
    }

    @RequestMapping("billView.do")
    public String billView(Model model, @RequestParam("billid") Long id) {
        Bill bill = billService.findById(id);
        model.addAttribute("bill", bill);
        return "billview";
    }

    @RequestMapping("billmodify")
    public String billModify(Model model, @RequestParam("billid") Long id) {
        Bill bill = billService.findById(id);
        model.addAttribute("bill", bill);
        List<Provider> providerList = providerService.findAll(null, null);
        model.addAttribute("providerList", providerList);
        return "billmodify";
    }

    @RequestMapping("getProviderList.do")
    @ResponseBody
    public String getProviderList() throws Exception {
        List<Provider> providerList = providerService.findAll(null, null);
        ObjectMapper om = new ObjectMapper();
        String s = om.writeValueAsString(providerList);
        return s;
    }


    @RequestMapping("billmodify.do")
    @Transactional
    public String doModifyBill(Model model, Bill bill) {
        System.out.println(bill);
        try {
            billService.modifyBill(bill);
        } catch (Exception e) {
            return "redirect:showBills.do";
        }
        return billView(model, bill.getId());
    }


    /**
     * 显示添加页面
     *
     * @return
     */
    @RequestMapping("billadd")
    @Transactional
    public String addBill() {
        return "billadd";
    }


    @RequestMapping("billAdd.do")
    @Transactional
    public String doBillAdd(Model model, Bill bill) {
        billService.save(bill);
        return "redirect:showBills.do";
    }

    @RequestMapping("delBill.do")
    @ResponseBody
    @Transactional
    public String doBillAdd(Model model,@RequestParam("billid") Long id) {
        Integer result = billService.delBill(id);
        String delResult = null;
        try {
            if (result == 0) {
                delResult = "notexist";
            } else {
                delResult = "true";
            }
        } catch (Exception e) {
            delResult = "false";
            return "{\"delResult\":\"" + delResult + "\"}";
        }
        return "{\"delResult\":\"" + delResult + "\"}";
    }
}
